package com.midou.gateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component

public class MyGatewayFilter extends ZuulFilter {
    @Override
    public String filterType() {
        //    pre：可以在请求被路由之前调用,拿到请求，在转发给对应的微服务之前的阶段
        //    route：在路由请求时候被调用，把请求转发给微服务之后的阶段
        //    post：在被代理的微服务返回结果后的阶段。
        //    error：被代理的微服务发生异常时的阶段
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        //返回一个boolean类型来判断该过滤器是否要执行，所以通过此函数可实现过滤器的开关。我们直接返回true，所以该过滤器总是生效
        return false;
    }
    //过滤器的具体执行逻辑
    @Override
    public Object run()
    {
        log.info("开始执行请求!!!");
        RequestContext ctx = RequestContext.getCurrentContext();
        //请求
        HttpServletRequest request = ctx.getRequest();
        /*处理跨域请求问题*/
        HttpServletResponse response = ctx.getResponse();
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST,DELETE,OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Accept,Origin,XRequestedWith,Content-Type,LastModified");
        /*判断请求路径是否是登录页面  是的话放行   不是就要验证令牌*/
//        if("/project-user/login".equals(request.getRequestURI())){
//            return null;
//        }else{
//            //暂时没有令牌
////            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
////            String token =  request.getHeader("jwt_token");
////            log.info("令牌是:"+token);
////            Algorithm algorithm = Algorithm.HMAC512(TransactionService.privateKey);
////            JWTVerifier verifier = JWT.require(algorithm).withIssuer("auto0").build();
////            Integer userId = null;
////            try {
////                DecodedJWT decodedJWT = verifier.verify(token);
////            } catch (Exception e){
////                log.info("验证令牌出错!");
////                tokenError(ctx);
////                return null;
////            }
//        }
//        log.info("发起的请求是:"+request.getRequestURI());

//       if("options".equals(ctx.getRequest().getMethod().toLowerCase())){                             //  感觉没用因运行没反应  比对的值是:post
//           log.info("请求不在转发");
//           ctx.setSendZuulResponse(false);
//           ctx.setResponseStatusCode(HttpStatus.OK.value());
//           return null;
//       }

        return null;
    }

    /*验证出错的后 阻止请求转发 返回401 没有权限验证码*/
    private void tokenError(RequestContext context){
        context.setSendZuulResponse(false);
        log.info("阻止请求继续");
        context.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
        log.info("响应401代码!");
    }
}
